How many days, hours, and minutes were between the last rain of the season (Malkosh) to the first (Yoreh)?
We need to divide our data into two: rainy_season_1 and rainy_season_2.
We need to find the time of the last rain in rainy_season_1.
We need to find the time of the first rain in rainy_season_2.
We need to compute the time difference between the two dates.
a possible solution
split_date ='2019-08-01'rainy_season_1 = df[:split_date] # everything before split daterainy_season_2 = df[split_date:] # everything after split datemalkosh = rainy_season_1['rain'].loc[rainy_season_1['rain'] >0].last_valid_index()yoreh = rainy_season_2['rain'].loc[rainy_season_2['rain'] >0].first_valid_index()dry_period = yoreh - malkosh# extracting days, hours, and minutesdays = dry_period.dayshours = dry_period.components.hoursminutes = dry_period.components.minutesprint(f'The dry period of 2019 was {days} days, {hours} hours and {minutes} minutes.')
What was the rainiest morning (6am-12pm) of the year? Bonus, what about the rainiest night (6pm-6am)?
We need to filter our data to contain only morning times.
We need to sum rain by day.
We need to find the day with the maximum value.
a possible solution
# filter to only day datamorning_df = df.loc[((df.index.hour >=6) & (df.index.hour <18))]morning_rain = morning_df['rain'].resample('D').sum()rainiest_morning = morning_rain.idxmax()# plotmorning_rain.plot()plt.axvline(rainiest_morning, c='r', alpha=0.5, linestyle='--')
bonus solution
# filter to only night datadf_night = df.loc[((df.index.hour <6) | (df.index.hour >=18))]# resampling night for each day is tricky because the date changes at 12:00. We can do this trick:# we shift the time back by 6 hours so all the data for the same night will have the same date.df_shifted = df_night.tshift(-6, freq='H')night_rain = df_shifted['rain'].resample('D').sum()rainiest_night = night_rain.idxmax()# plotnight_rain.plot()plt.axvline(rainiest_night, c='r', alpha=0.5, linestyle='--')
Note: this whole webpage is actually a Jupyter Notebook rendered as html. If you want to know how to make interactive graphs, go to the top of the page and click on “ Code”
Useful functions compatible with pandas.resample() can be found here. The full list of resampling frequencies can be found here.